﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================
// Original Author: Guy Smith-Ferrier
// Date Created   : September 2005
// Notes          : This file is part of the downloadable source code for .NET Internationalization, by
//                  Guy Smith-Ferrier, published by Addison-Wesley. See http://www.dotneti18n.com for details.
// Disclaimer     : No warranty is provided. Use at your own risk.
// Modification   : You are free to modify this code providing that this block of comments is not altered
//                  and is placed before any code.
//===============================================================================

using System;
using System.Resources;
using System.Globalization;
using EntLibContrib.Resource;
using EntLibContrib.Resource.Managers;

namespace EntLibContrib.Resource.Web
{
	/// <summary>
	/// WebResourceProvider is an System.Web.Compilation.IResourceProvider class which provides some common functionality
	/// used by resource providers for the web
	/// </summary>
	public class WebResourceProvider : System.Web.Compilation.IResourceProvider
	{
		#region Fields
		private ResourceManager resourceManager;
		private string resourceManagerName;
		#endregion

		#region Properties
		/// <summary>
		/// ResourceManager is the resource manager used by the resource provider
		/// </summary>
		public ResourceManager ResourceManager
		{
			get
			{
				if (resourceManager == null)
				{
					if (String.IsNullOrEmpty(resourceManagerName))
						resourceManager = ResourceFactory.GetResourceManager();
					else
						resourceManager = ResourceFactory.GetResourceManager(resourceManagerName);
				}

				return resourceManager;
			}
		}

		/// <summary>
		/// ResourceReader is the IResourceReader provided by the resource provider
		/// </summary>
		public IResourceReader ResourceReader
		{
			get 
			{
				CommonResourceSet resourceSet = (CommonResourceSet)ResourceManager.GetResourceSet(resourceManager.CultureInfo);
				return resourceSet.CreateDefaultReader(); 
			}
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="WebResourceProvider"/> class.
		/// </summary>
		/// <param name="resourceManagerName">Name of the resource manager.</param>
		public WebResourceProvider(string resourceManagerName)
		{
			this.resourceManagerName = resourceManagerName;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Gets the value of the specified Object resource for the specified culture
		/// </summary>
		/// <param name="resourceKey">The name of the resource to get</param>
		/// <param name="culture">The CultureInfo object that represents the culture for which the resource is
		/// localized</param>
		/// <returns>
		/// The value of the resource, localized for the specified culture
		/// </returns>
		public object GetObject(string resourceKey, CultureInfo culture)
		{
			return ResourceManager.GetObject(resourceKey, culture);
		}
		#endregion
	}
}
